{% extends "dvaui/base.html" %}
{% load staticfiles %}

{% block page_content %}
<section class="content-header" style="padding-top:0px" xmlns="http://www.w3.org/1999/html">
<div class="row">
    <div class="box box-info">
        <div class="box-header">
            <h2>Monitoring, Workers & Queue management</h2>
        </div>
    </div>
</div>
</section>
<section class="content">
    <div class="row">
        <div class="col-lg-12">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h4>Management action log</h4>
            </div>
            <div class="box-body">
                <table class="table dataTables">
                <thead>
                <tr>
                    <th>op</th>
                    <th>host</th>
                    <th>message</th>
                    <th>time</th>
                </tr>
                </thead>
                <tbody>
                    {% for k in actions %}
                        {% if k.message %}
                            <tr>
                                <td>{{ k.op }} </td>
                                <td>{{ k.host }}</td>
                                <td>{{ k.message }}</td>
                                <td>{{ k.created|timesince }}</td>
                            </tr>
                        {% endif %}
                    {% endfor %}
                </tbody>
                </table>
            </div>
        </div>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h4>Task Restarts</h4>
            </div>
            <div class="box-body">
                <table class="table dataTables-dict">
                <thead>
                <tr>
                    <th>Original attempt pk</th>
                    <th>Latest attempt pk</th>
                    <th>Process</th>
                    <th>Attempts</th>
                    <th>Re-submitted</th>
                    <th>ts</th>
                </tr>
                </thead>
                <tbody>
                    {% for t in restarts %}
                    <tr>
                        <td>{{ t.original_event_pk }} </td>
                        <td><a href="/task_detail/{{ t.launched_event_pk }}">current attmpt</a></td>
                        <td><a href="/process/{{ t.process_id }}">{{ t.process_id }}</a></td>
                        <td class="text-right">{{ t.attempts }}</td>
                        <td>{{ t.created|timesince }} ago</td>
                        <td>{{ t.created }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
                </table>
            </div>
        </div>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
            <div class="box box-primary">
                <div class="box-header"><h2>Workers</h2></div>
                <div class="box-body">
                <table class="table dataTables">
                <thead>
                <tr>
                    <th>queue_name</th>
                    <th>host</th>
                    <th>pid</th>
                    <th>Alive</th>
                    <th>Started</th>
                    <th>Started since</th>
                    <th>Last ping</th>
                    <th>Last ping since</th>
                </tr>
                </thead>
                <tbody>
                    {% for k in workers %}
                        <tr>
                            <td>{{ k.queue_name }} </td>
                            <td>{{ k.host }}</td>
                            <td>{{ k.pid }}</td>
                            <td>{{ k.alive }}</td>
                            <td>{{ k.created }}</td>
                            <td>{{ k.created|timesince }}</td>
                            <td>{{ k.last_ping }}</td>
                            <td>{{ k.last_ping|timesince }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
                </table>
                </div>
            </div>
        </div>
        <div class="col-lg-12">
            <div class="box box-primary">
                <div class="box-header"><h2>System state</h2></div>
                <div class="box-body">
                <table class="table dataTables">
                <thead>
                <tr>
                    <th>id</th>
                    <th>Processes</th>
                    <th>Total / Pending / Completed tasks</th>
                    <th>Redis hits / misses</th>
                    <th>Redis expired / evicted keys</th>
                    <th>Redis clients</th>
                    <th>Redis memory</th>
                    <th>Workers alive / transition-to-dead / dead  </th>
                    <th>Timestamp</th>
                    <th>Since</th>
                </tr>
                </thead>
                <tbody>
                    {% for k in state %}
                        <tr>
                            <td>{{ k.pk }} </td>
                            <td>{{ k.process_stats.processes }} </td>
                            <td>{{ k.process_stats.tasks }} / {{ k.process_stats.pending_tasks }} / {{ k.process_stats.completed_tasks }}</td>
                            <td>{{ k.redis_stats.keyspace_hits }} / {{ k.redis_stats.keyspace_misses }}</td>
                            <td>{{ k.redis_stats.expired_keys }} / {{ k.redis_stats.evicted_keys }}</td>
                            <td>{{ k.redis_stats.connected_clients }}</td>
                            <td>{{ k.redis_stats.used_memory_human }}</td>
                            <td>{{ k.worker_stats.alive }} / {{ k.worker_stats.transition }} / {{ k.worker_stats.dead }}</td>
                            <td>{{ k.created }}</td>
                            <td>{{ k.created|timesince }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
                </table>
                </div>
            </div>
        </div>
    </div>
</section>
{% endblock %}
{% block page_js%}
<script>
    $(document).ready(function() {
        InitializeTables();
    })
</script>
{% endblock %}
